package com.xpf.controller.center;

import com.xpf.controller.BaseController;
import com.xpf.enums.YesOrNo;
import com.xpf.pojo.OrderItems;
import com.xpf.pojo.Orders;
import com.xpf.pojo.bo.center.OrderItemsCommentBO;
import com.xpf.service.center.MyCommentsService;
import com.xpf.service.center.MyOrdersService;
import com.xpf.utils.JSONResult;
import com.xpf.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用户中心——评价模块",tags = "用户中心——评价模块")
@RestController
@RequestMapping("mycomments")
public class MyCommentsController extends BaseController {

    @Autowired
    private MyCommentsService myCommentsService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @PostMapping("/pending")
    public JSONResult query(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId",value = "订单id",required = false)
            @RequestParam String orderId
            ){

        //判断用户和订单是否有关联
        JSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }

        //判断订单是否评价过
        Orders myOrder = (Orders) checkResult.getData();
        if (myOrder.getIsComment() == YesOrNo.YES.type){
            return JSONResult.errorMsg("该笔订单已经评价");
        }

        List<OrderItems> orderItemsList = myCommentsService.queryPendingComment(orderId);
        return JSONResult.ok(orderItemsList);
    }

    @ApiOperation(value = "保存评论列表", notes = "保存评论列表")
    @PostMapping("/saveList")
    public JSONResult saveList(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId",value = "订单id",required = false)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList
            ){
        //判断用户和订单是否有关联
        JSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }
        //判断评论内容list不能为空
       if (commentList == null || commentList.isEmpty() || commentList.size() == 0){
           return JSONResult.errorMsg("评论内容不能为空！");
       }
       myCommentsService.saveComments(orderId,userId,commentList);
        return JSONResult.ok();
    }

    @ApiOperation(value = "查询我的评价", notes = "查询我的评价")
    @PostMapping("/query")
    public JSONResult query(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "page",value = "当前第几页",required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize",value = "每页条数",required = false)
            @RequestParam Integer pageSize){

        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg(null);
        }
        if (page == null)
            page = 1;
        if (pageSize == null)
            pageSize = COMMON_PAGE_SIZE;

        PagedGridResult gridResult = myCommentsService.queryMyComments(userId,page,pageSize);
        return JSONResult.ok(gridResult);
    }

}
